package me.xingyannian.zhaopin.controller;

import me.xingyannian.zhaopin.entity.RegionDegreeJobStatistics;
import me.xingyannian.zhaopin.entity.SubjectStageJobStatistics;
import me.xingyannian.zhaopin.service.ChangCiService;
import me.xingyannian.zhaopin.service.JobService;
import me.xingyannian.zhaopin.service.JobStatisticsService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

/**
 * 岗位统计控制器
 *
 * @author XING&WANG
 * @version 2025/05/03 22:30
 **/
@Controller
@RequestMapping("/job/statistics")
public class JobStatisticsController {
    private final JobStatisticsService jobStatisticsService;
    private final ChangCiService changCiService;
    private final JobService jobService;

    public JobStatisticsController(JobStatisticsService jobStatisticsService, ChangCiService changCiService, JobService jobService) {
        this.jobStatisticsService = jobStatisticsService;
        this.changCiService = changCiService;
        this.jobService = jobService;
    }
    // 归属-学历-岗位统计页面 http://localhost:8080/job/statistics/regin-degree?changCiId=1
    @GetMapping("/region-degree")
    public String regionDegreeJobStatistics(Model model, @RequestParam Integer changCiId) {
        // 1. 根据场次查询统计数据
        var opt = this.changCiService.findById(Long.valueOf(changCiId));
        if (opt.isEmpty()) {
            model.addAttribute("error", true);
            model.addAttribute("errorMsg", "场次不存在");
            return "job/rdjs";
        }
        var changCi = opt.get();
        model.addAttribute("changCi", changCi);
        // 2. 根据场次查询归属-学历-岗位数据
        var rdjsList = this.jobStatisticsService.findRDJSByChangCi(changCi);
        // 3. 如果已经有分析数据，则直接返回
        if (rdjsList != null && !rdjsList.isEmpty()) {
            model.addAttribute("rdjsList", rdjsList);
            Integer undergraduateSum = 0;
            Integer postgraduateSum = 0;
            Integer totalSum = 0;
            // 统计时间
            var sTime = rdjsList.get(0).getCreateTime();
            model.addAttribute("sTime", sTime);
            for (var rdjs : rdjsList) {
                undergraduateSum += rdjs.getUndergraduateJobCount();
                postgraduateSum += rdjs.getPostgraduateJobCount();
                totalSum += rdjs.getTotalJobCount();
            }
            model.addAttribute("undergraduateSum", undergraduateSum);
            model.addAttribute("postgraduateSum", postgraduateSum);
            model.addAttribute("totalSum", totalSum);
            return "job/rdjs";
        }
        // 4. 处理数据
        // 数据初始化
        List<RegionDegreeJobStatistics> rdjsInitList = new ArrayList<>();
        // 4.1 根据场次查询岗位表归属无重复list
        var regionList = this.jobService.findDistinctRegionByChangCi(changCi);
        for (var region : regionList) {
            RegionDegreeJobStatistics rdjs = new RegionDegreeJobStatistics();
            rdjs.setChangCi(changCi);
            rdjs.setRegion(region);
            // 4.2 统计每个归属下本科学历和研究生学历的岗位数量
            Integer undergraduateJobCount1 = this.jobService.countByChangCiAndRegionAndDegree("本科", changCi, region);
            Integer undergraduateJobCount2 = this.jobService.countByChangCiAndRegionAndDegree("本科及以上", changCi, region);
            rdjs.setUndergraduateJobCount(undergraduateJobCount1 + undergraduateJobCount2);
            Integer postgraduateJobCount = this.jobService.countByChangCiAndRegionAndDegree("研究生", changCi, region);
            rdjs.setPostgraduateJobCount(postgraduateJobCount);
            // 4.3 总计
            Integer totalCount = undergraduateJobCount1 + undergraduateJobCount2 + postgraduateJobCount;
            rdjs.setTotalJobCount(totalCount);
            rdjsInitList.add(rdjs);
        }
        // 写入数据库
        this.jobStatisticsService.saveRDJSAll(rdjsInitList);
        // 刷新页面
        return "redirect:/job/statistics/region-degree?changCiId=" + changCiId;
    }
    // 重新统计
    @GetMapping("/region-degree/reset")
    public String reReginDegreeStatistics(@RequestParam Integer changCiId) {
        // 1. 根据场次查询统计数据
        var opt = this.changCiService.findById(Long.valueOf(changCiId));
        if (opt.isEmpty()) {
            return "redirect:/error?error=true&errorMsg=" + "场次不存在";
        }
        var changCi = opt.get();
        // 2. 清空数据库中的统计数据
        this.jobStatisticsService.deleteRDJSByChangCi(changCi);
        return "redirect:/job/statistics/region-degree?changCiId=" + changCiId;
    }

    // 学科-学段-岗位统计 http://localhost:8080/job/statistics/subject-stage?changCiId=1
    @GetMapping("/subject-stage")
    public String subjectStageJobStatistics(@RequestParam Integer changCiId, Model model) {
        // 1. 数据库查询数据，如果有分析数据，则直接返回，如果没有分析数据，则进行数据处理
        // 1.1 根据场次查询统计数据
        var opt = this.changCiService.findById(Long.valueOf(changCiId));
        if (opt.isEmpty()) {
            model.addAttribute("error", true);
            model.addAttribute("errorMsg", "场次不存在");
            return "job/ssjs";
        }
        var changCi = opt.get();
        model.addAttribute("changCi", changCi);
        // 1.2 根据场次查询学科学段岗位数据
        var ssjsList = this.jobStatisticsService.findSSJSByChangCi(changCi);
        // 1.3 如果已经有分析数据，则直接返回
        if (ssjsList != null && !ssjsList.isEmpty()) {
            // 统计时间
            var sTime = ssjsList.get(0).getCreateTime();
            model.addAttribute("sTime", sTime);
            model.addAttribute("ssjsList", ssjsList);
            // 1.4 不同学段求和
            Integer primarySum = 0;
            Integer juniorSum = 0;
            Integer compulsorySum = 0;
            Integer seniorSum = 0;
            Integer vocationalSum = 0;
            Integer totalSum = 0;
            for (var ssjs : ssjsList) {
                // 1.4.1 求和
                primarySum += ssjs.getPrimaryJobCount();
                juniorSum += ssjs.getJuniorHSJobCount();
                compulsorySum += ssjs.getCompulsoryEdJobCount();
                seniorSum += ssjs.getSeniorHSJobCount();
                vocationalSum += ssjs.getVocationalJobCount();
                totalSum += ssjs.getTotalJobCount();
            }
            // 1.4.2 设置属性
            model.addAttribute("primarySum", primarySum);
            model.addAttribute("juniorSum", juniorSum);
            model.addAttribute("compulsorySum", compulsorySum);
            model.addAttribute("seniorSum", seniorSum);
            model.addAttribute("vocationalSum", vocationalSum);
            model.addAttribute("totalSum", totalSum);
            return "job/ssjs";
        }
        // 2. 处理数据
        // 2.0 初始化数据
        List<SubjectStageJobStatistics> ssjsInitList = new ArrayList<>();
        // 2.1 根据场次查询岗位表学科无重复list
        var subjectList = this.jobService.findDistinctSubjectByChangCi(changCi);
        for (var subject : subjectList) {
            SubjectStageJobStatistics ssjs = new SubjectStageJobStatistics();
            ssjs.setChangCi(changCi);
            ssjs.setSubject(subject);
            // 2.2 统计每个学科下每个学段的岗位数量
            var pjc = this.jobService.countByChangCiAndSubjectAndStage(changCi, subject, "小学");
            ssjs.setPrimaryJobCount(pjc);
            var jhsjc = this.jobService.countByChangCiAndSubjectAndStage(changCi, subject, "初中");
            ssjs.setJuniorHSJobCount(jhsjc);
            var cejc = this.jobService.countByChangCiAndSubjectAndStage(changCi, subject, "义务教育");
            ssjs.setCompulsoryEdJobCount(cejc);
            var shsjc = this.jobService.countByChangCiAndSubjectAndStage(changCi, subject, "高中");
            ssjs.setSeniorHSJobCount(shsjc);
            var vjc = this.jobService.countByChangCiAndSubjectAndStage(changCi, subject, "中职");
            ssjs.setVocationalJobCount(vjc);
            var tj = pjc + jhsjc + cejc + shsjc + vjc;
            ssjs.setTotalJobCount(tj);
            ssjsInitList.add(ssjs);
        }
        // 3. 写入数据库
        this.jobStatisticsService.saveSSJSAll(ssjsInitList);
        // 4. 刷新页面
        return "redirect:/job/statistics/subject-stage?changCiId=" + changCiId;
    }
    // 重新统计
    @GetMapping("/subject-stage/reset")
    public String reSubjectStageStatistics(@RequestParam Integer changCiId) {
        // 1. 根据场次查询统计数据
        var opt = this.changCiService.findById(Long.valueOf(changCiId));
        if (opt.isEmpty()) {
            return "redirect:/error?error=true&errorMsg=" + "场次不存在";
        }
        var changCi = opt.get();
        // 2. 清空数据库中的统计数据
        this.jobStatisticsService.deleteSSJSByChangCi(changCi);
        return "redirect:/job/statistics/subject-stage?changCiId=" + changCiId;
    }

}
